#!/bin/sh

if ! which commitlint > /dev/null; then
  echo "ERROR: 'commitlint' is required. Please install it, using at your convenience:"
  echo "  $ npm install -g @commitlint/cli @commitlint/config-conventional"
  echo "  $ yarn global add @commitlint/cli @commitlint/config-conventional"
  exit 1
fi

CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)

if [ x${CI_COMMIT_REF_NAME} = xmaster -o x${CURRENT_BRANCH} = xmaster ]; then
  commitlint --from=HEAD~5
else
  if [ -n "${GITLAB_CI}" ]; then
    git fetch -q origin master
    commitlint --from=origin/master
  else
    commitlint --from=master
  fi
fi
